# install jdk

class java::jdk {

    $java_base = "/opt/java"
    $java_file = "jdk-7u25-linux-x64.gz"
    $java_version = "1.7.0_25"

    Exec { path => "/bin" }

    # Creates the path for Java installation
    file { "$java_base":
        ensure => "directory",
        owner => "root",
        group => "root",
        alias => "java-base"
    }

    file { "$java_base/$java_file":
        mode => "0644",
        owner => "root",
        group => "root",
        source => "/vagrant/modules/java/files/$java_file",
        alias => "java-source-file",
        before => Exec["untar-java"],
        require => File["java-base"]
    }

    file { "$java_base/select-java.sh":
        mode => "0755",
        owner => "root",
        group => "root",
        source => "/vagrant/modules/java/files/select-java.sh",
        alias => "select-java",
        before => Exec["untar-java"],
        require => File["java-base"]
    }

    exec { "untar $java_file":
        command => "tar -zxf $java_file",
        cwd => "$java_base",
        creates => "$java_base/jdk$java_version",
        alias => "untar-java",
        refreshonly => true,
        subscribe => File["java-source-file"],
        before => File["java-app-dir"]
    }

    file { "$java_base/jdk$java_version":
        ensure => "directory",
        mode => "0644",
        owner => "root",
        group => "root",
        alias => "java-app-dir"
    }

    ############################################
    # TODO: This script doesn't work, need to fix
    ############################################
    exec {"select-java.sh":
        command => "/usr/bin/sudo ./select-java.sh",
        cwd => "$java_base",
        require => File["select-java"]
    }
}